package com.zy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.entity.User;
import com.zy.service.RoleService;
import com.zy.service.UserService;
import com.zy.vo.DataView;
import com.zy.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @Autowired
    private RoleService roleService;


    /**
     * 分页查询所有用户数据带有查询条件
     * @param userVo
     * @return
     */
    @RequestMapping("/loadAllUser")
    @ResponseBody
    public DataView getAllUser(UserVo userVo){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        IPage<User> page = new Page<>(userVo.getPage(), userVo.getLimit());
        queryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(userVo.getUsername()), "username", userVo.getUsername());
//        queryWrapper.eq(StringUtils.isNotBlank(userVo.getId()), "phone", userVo.getId());
        IPage<User> ipage = userService.page(page, queryWrapper);

        DataView dataView = new DataView(ipage.getTotal(), ipage.getRecords());
        dataView.setCode(0);
        return dataView;


    }

    /**
     * 新增用户
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public DataView addUser(User user){
        boolean save=userService.save(user);
        DataView dataView=new DataView();
        dataView.setMsg("添加用户成功");
        dataView.setCode(200);
        return dataView;
    }


    /**
     * 修改用户
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public DataView updataUser(User user){
        userService.updateById(user);
        DataView dataView = new DataView();
        dataView.setMsg("修改用户成功");
        dataView.setCode(200);
        return dataView;
    }

    /**
     * 删除用户
     */
    @RequestMapping("/deleteUser/{id}")
    @ResponseBody
    public DataView deleteUser(@PathVariable("id")Integer id){
        userService.removeById(id);
        DataView dataView = new DataView();
        dataView.setMsg("删除用户成功");
        dataView.setCode(200);
        return dataView;
    }

    /**
     * 重置密码
     */
    @RequestMapping("/resetPwd/{id}")
    @ResponseBody
    public DataView resetPwd(@PathVariable("id") Integer id){
        User user = new User();
        user.setId(id);
        user.setPassword("123456");
        userService.updateById(user);
        DataView dataView=new DataView();
        dataView.setMsg("重置密码成功");
        dataView.setCode(200);
        return dataView;
    }

    /**
     * 点击分配的时候，初始化用户角色
     * 打开分配角色的弹出层
     * 根据id查询所拥有的角色
     */
    @RequestMapping("/initRoleByUserId")
    @ResponseBody
    public DataView initRoleByUserId(Integer id){
        //1.查询所有角色
        List<Map<String,Object>> listMaps = roleService.listMaps();;
        //2.查询当前登录用户所拥有的角色
        List<Integer> currentUserRoleIds= roleService.queryUserRoleById(id);
        //3.让你的前端变为选中状态
        for (Map<String,Object> map : listMaps){
            Boolean LAY_CHECKED = false;
            Integer roleId = (Integer) map.get("id");
            for (Integer rid : currentUserRoleIds){
                if (rid.equals(roleId)) {
                    LAY_CHECKED = true;
                    break;
                }
            }
            map.put("LAY_CHECKED",LAY_CHECKED);
        }
        DataView dataView = new DataView(Long.valueOf(listMaps.size()),listMaps);
        dataView.setCode(0);
        return dataView;
    }

    /**
     * 保存用户与角色之间的关系  1对多
     * 先删除再保存关系
     */
    @RequestMapping("/saveUserRole")
    @ResponseBody
    public DataView saveUserRole(Integer uid,Integer[] ids){
        userService.saveUserRole(uid,ids);
        DataView dataView = new DataView();
        dataView.setMsg("用户角色分配成功！");
        dataView.setCode(200);
        return dataView;
    }


}
